#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @Time    : 2018/1/24 15:18
# @Author  : RNone
import xlrd
from xlutils.copy import copy


class excel_control():
    def excel_read(self, path, index=0):
        '''
        读取excel文件，并返回sheet
        :param path: 文件路径
        :param index: 工作表索引
        :return: sheet对象
        '''
        data = xlrd.open_workbook(path)
        sheet = data.sheets()[index]
        return sheet

    def excel_revise(self, path, row, column, data):
        '''
        修改指定位置内容
        :param path: 文件路径
        :param row: 横坐标索引
        :param column: 纵坐标索引
        :param data: 修改内容
        :return: 
        '''
        rb = xlrd.open_workbook(path)
        rs = rb.sheet_by_index(0)
        wb = copy(rb)
        ws = wb.get_sheet(0)
        ws.write(int(row), int(column), data)
        wb.save(path)

    def case_to_dict(self, keys, values):
        '''
        将用例字典化
        :param keys: 用例的标题列表
        :param values: 用例的值列表
        :return: 用例的标题与值对应的字典
        '''
        case = {}
        for i in range(len(keys)):
            if isinstance(values[i], float):
                values[i] = int(values[i])
            case[keys[i]] = values[i]
        return case

    def params_to_dict(self, case):
        '''
        将用例中parms字典化并数字整型化
        :param case: 用例字典
        :return: 用例字典
        '''
        params = {}
        temp = case['params'].split('\n')
        for i in temp:
            if i.split('=')[1].isdigit():
                value = int(i.split('=')[1])
            else:
                value = i.split('=')[1]
            params[i.split('=')[0]] = value
        case['params'] = params
        return case

    def case_to_dict_from_cases(self, keys, values):
        '''
        针对phone_api的用例字典化的全部流程，包括parms字典化和用例数字整型化
        :param keys: 用例的标题列表
        :param values: 用例的值列表
        :return: 用例的标题与值对应的字典
        '''
        case = self.case_to_dict(keys, values)  # 将用例参数取出，并用变量名匹配
        case = self.params_to_dict(case)  # 将参数字典化
        return case
